package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceSaveParam;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public List<GeofenceVO> selectGeo(GeofenceQuery geofenceQuery){
        log.debug("查询电子围栏业务：{}",geofenceQuery);
        List<GeofenceVO> list=geofenceMapper.selectGeo(geofenceQuery);
        //需要补全每个电子围栏的数据
        //1.遍历取出每个围栏V0
        for(int i=0;i< list.size();i++){
            GeofenceVO geofenceVO=list.get(i);
            //2.准备车辆查询的DT0对象VehicleQuery
            VehicleQuery vehicleQuery=new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList=vehicleMapper.selectVehicle(vehicleQuery);
            //3.获取当前围栏上绑定的车辆总数
            int totalNum=vehicleVOList.size();
            //4.获取当前围栏上的可用车辆数
            int availableNum=0;
            for (VehicleVO vehicleVO:vehicleVOList){
                if(vehicleVO.getStatus().equals("1")){
                    availableNum++;
                }
            }
            //5.给当前围栏v0补全车辆数据
            geofenceVO.setTotalNum(totalNum);
            geofenceVO.setAvailableNum(availableNum);
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }

    @Override
    public void saveGeo(GeofenceSaveParam geofenceSaveParam) {
        log.debug("保存电子围栏业务：{}",geofenceSaveParam);
        Geofence  geofence=new Geofence();
        BeanUtils.copyProperties(geofenceSaveParam,geofence);
        if(geofence.getId()==null) {//新增
            geofence.setStatus("1");//启用
            geofence.setCreateTime(new Date());
            geofenceMapper.insert(geofence);
        }else {//更新
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("删除电子围栏业务：{}",id);
        //先查这个围栏上是否有绑定的车辆
        //1.准备车辆查询的DTO对象
        VehicleQuery vehicleQuery=new VehicleQuery();
        //2.将查询条件（围栏ID）封装到DTO中
        vehicleQuery.setGeofenceId(id);
        //3.调用mapper层的方法,查的指定国栏上的车辆
        List<VehicleVO> list=vehicleMapper.selectVehicle(vehicleQuery);
        //4.判断车辆列表是否为空
        if(list.size()>0 && list !=null) {//当前围栏上有绑定车辆
            throw new ServiceException(StatusCode.VEHICLE_EXISTS);//注意，此状态码需要新写一个
        }else {//该围栏上没有绑定车辆，正常删除
            geofenceMapper.delete(id);
            log.debug("删除成功！");
        }
    }
}
